package com.qys.livesMall.shortVideo.controller.app;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.app.user.model.dto.AppBaseDTO;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.shop.modal.entity.ShopGoodsInfo;
import com.qys.livesMall.shortVideo.model.dto.app.AppShortVideoPageDTO;
import com.qys.livesMall.shortVideo.model.dto.app.AppShortVideoSaveDTO;
import com.qys.livesMall.shortVideo.model.entity.H2ShortVideoCategory;
import com.qys.livesMall.shortVideo.model.entity.ShortVideoHistory;
import com.qys.livesMall.shortVideo.model.enums.ShortVideoEventTypeEnum;
import com.qys.livesMall.shortVideo.model.vo.app.*;
import com.qys.livesMall.shortVideo.service.IH2ShortVideoCategoryService;
import com.qys.livesMall.shortVideo.service.IH2ShortVideoService;
import com.qys.livesMall.shortVideo.service.ShortVideoHistoryService;
import com.qys.livesMall.shortVideo.service.ShortVideoUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * app-短视频.
 * User: dp
 * Date: 2023/05/05
 * Time: 13:47
 * DESCRIPTION:
 */
@RestController
@Tag(name = "app-短视频")
@RequestMapping("/app/short/video")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppShortVideoController {

    private final IH2ShortVideoService shortVideoService;
    private final ShortVideoUserService shortVideoUserService;
    private final ShortVideoHistoryService shortVideoHistoryService;

    private final IH2ShortVideoCategoryService shortVideoCategoryService;

    @GetMapping
    @Operation(summary = "获取短视频信息")
    public R<AppShortVideoVO> getShortVideo() {
        return R.ok(shortVideoService.getShortVideoByApp());
    }

    @PutMapping("/like/{videoId}")
    @Operation(summary = "点赞")
    public R<Void> likeVideo(@PathVariable String videoId) {
        shortVideoUserService.userLikeOrCollection(videoId, ShortVideoEventTypeEnum.LIKE);
        return R.ok();
    }

    @PutMapping("/collection/{videoId}")
    @Operation(summary = "收藏")
    public R<Void> collectionVideo(@PathVariable String videoId) {
        shortVideoUserService.userLikeOrCollection(videoId, ShortVideoEventTypeEnum.COLLECTION);
        return R.ok();
    }

    @PutMapping("/forward/{videoId}")
    @Operation(summary = "转发")
    public R<Void> forwardVideo(@PathVariable String videoId) {
        shortVideoService.forwardVideo(videoId);
        return R.ok();
    }

    @PostMapping("/insert")
    @Operation(summary = "用户上传短视频信息")
    public R<Void> save(@RequestBody @Validated AppShortVideoSaveDTO shortVideoSaveDTO) {
        shortVideoService.saveVideoByApp(shortVideoSaveDTO);
        return R.ok();
    }

    @GetMapping({"/{id}", "/{id}/{history}"})
    @Operation(
            summary = "获取短视频信息",
            parameters = {
                    @Parameter(name = "id", description = "短视频id"),
                    @Parameter(name = "history", description = "是否从浏览历史记录跳转")
            }
    )
    public R<AppShortVideoVO> getShortVideoById(@PathVariable Long id, @PathVariable(required = false) Byte history) {
        return R.ok(shortVideoService.getShortVideoById(id, history));
    }

    @PostMapping("/page")
    @Operation(summary = "用发布短视频查询")
    public R<Page<AppShortVideoVO>> page(@RequestBody BaseQuery baseQuery) {
        baseQuery.setId(String.valueOf(SecurityUtils.getUserId()));
        return R.ok(shortVideoService.pageByApp(baseQuery));
    }

    /**
     * 记录用户长时间停留接口
     */
    @GetMapping("/recordUserHotspot")
    @Operation(summary = "记录用户特别关注视频")
    public R<Void> recordUserHotspot(@RequestParam String videoId) {
        LambdaUpdateWrapper<ShortVideoHistory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ShortVideoHistory::getUserId,SecurityUtils.getUserId());
        updateWrapper.eq(ShortVideoHistory::getVideoId,videoId);

        ShortVideoHistory shortVideoHistory = new ShortVideoHistory();
        shortVideoHistory.setFollow(1);
        shortVideoHistoryService.update(shortVideoHistory, updateWrapper);
        return R.ok();
    }

    /**
     * 分页获取短视频列表（app首页推荐）
     */
    @PostMapping("/recommendedList")
    @Operation(summary = "分页获取短视频列表（app首页推荐）")
    public R<Page<AppUserCollectionLikeVO>> recommendedList(@RequestBody AppShortVideoPageDTO queryDTO){
        return R.ok(shortVideoService.recommendedPage(queryDTO));
    }

    @PutMapping("/getFollowUserShortVideo")
    @Operation(summary = "获取短视频信息")
    public R<AppShortVideoVO> getFollowUserShortVideo() {
        return R.ok(shortVideoService.getFollowUserShortVideo());
    }

    @GetMapping("/hasShop")
    @Operation(summary = "判断当前用户是否是店主")
    public R<Boolean> hasShop() {
        return R.ok(shortVideoService.hasShop(SecurityUtils.getUserId()));
    }

    @PostMapping("/getShopGoodsInfoByVideo")
    @Operation(summary = "获取视频绑定的商品")
    public R<IPage<ShopGoodsInfo>> getShopGoodsInfoByVideo(@RequestBody BaseQuery baseQuery) {
        return R.ok(shortVideoService.getShopGoodsInfoByVideo(baseQuery));
    }

    @GetMapping("/querykeywords")
    @Operation(
            summary = "视频搜索关键字",
            parameters = {
                    @Parameter(name = "keywords", description = "关键字")
            }
    )
    public R<List<QuerykeywordsVO>> querykeywords(@RequestParam(value = "keywords", required = false) String keywords) {
        return R.ok(shortVideoService.querykeywords(keywords));
    }

    @PostMapping("/queryTheListOfLikedVideos")
    @Operation(summary = "查询被点赞的视频列表")
    public R<IPage<LikedVideoVO>> queryTheListOfLikedVideos(@RequestBody AppBaseDTO dto) {
        return R.ok(shortVideoService.queryTheListOfLikedVideos(dto));
    }

    @PostMapping("/queryTheListOfCommentVideos")
    @Operation(summary = "查询被评论的视频列表")
    public R<IPage<CommentVideoVO>> queryTheListOfCommentVideos(@RequestBody AppBaseDTO dto) {
        return R.ok(shortVideoService.queryTheListOfCommentVideos(dto));
    }

    @PostMapping("/viewCommentsBasedOnVideoId/{videoId}")
    @Operation(summary = "根据视频id查看评论")
    public R<IPage<CommentVideoVO>> viewCommentsBasedOnVideoId(@PathVariable Long videoId, @RequestBody AppBaseDTO dto) {
        return R.ok(shortVideoService.viewCommentsBasedOnVideoId(videoId, dto));
    }

    @PostMapping("/category/page")
    @Operation(summary = "分页查询分类信息")
    public R<Page<H2ShortVideoCategory>> categoryPage(@RequestBody BaseQuery queryDTO) {
        return R.ok(shortVideoCategoryService.page(queryDTO));
    }

}
